package work.comeback.omo.controller.servlet.movie;

import java.io.IOException;
import java.io.Serial;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import work.comeback.omo.dao.movie.IMovieDetail;
import work.comeback.omo.dao.movie.IMovieInfo;
import work.comeback.omo.model.movie.detail.MovieDetailData;
import work.comeback.omo.model.movie.display.MovieCard;
import work.comeback.omo.model.movie.info.MovieVoidData;
import work.comeback.omo.service.MovieService;
import work.comeback.omo.service.imp.MovieServiceImpl;

@WebServlet(name = "MovieDetailApi", value = "/api/movie/detail")
public class DetailServlet extends HttpServlet {
    private static final Logger logger = LoggerFactory.getLogger(DetailServlet.class);

    @Serial
    private static final long serialVersionUID = 1L;
    private final MovieService movieService = new MovieServiceImpl();
    private final IMovieInfo movieInfoService = movieService.getMovieInfo();
    private final IMovieDetail movieDetailService = movieService.getMovieDetail();

    private void errorParamsToHome(String msg, HttpServletResponse resp) throws IOException {
        logger.warn(msg);
        resp.sendRedirect("/home");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        String movieId = req.getParameter("movieId");

        if (movieId == null || movieId.equals("0")) {
            errorParamsToHome("movieId is null", resp);
            return;
        }

        int searchMovieId = Integer.parseInt(movieId);

        // 影片基础信息
        MovieVoidData movieInfoData = movieInfoService.getMovieInfoById(searchMovieId);
        if (movieInfoData == null) {
            errorParamsToHome("movie data is null", resp);
            return;
        }
        // 获取电影详情数据
        MovieCard playerCard = new MovieCard(movieInfoData, movieDetailService);
        // 详情数据
        MovieDetailData detailData = playerCard.getMovieDetailData();

        // 同类型影片
        String typeName = movieInfoData.getTypeName();
        ArrayList<MovieCard> detailLoveCards = new ArrayList<>();
        List<MovieVoidData> moviesByTypeName = movieInfoService.getMoviesByTypeName(typeName, 4);
        moviesByTypeName.forEach(movieVoidData -> {
            if (detailLoveCards.size() != 6) {
                MovieCard card = new MovieCard(movieVoidData, movieDetailService);
                if (!card.isNull()) {
                    detailLoveCards.add(card);
                }
            }
        });

        // 设置参数
        req.setAttribute("hasMovieData", true); // 标志位
        req.setAttribute("movieLoveCards", detailLoveCards); // 当前播放页面的猜你喜欢
        req.setAttribute("moviePlayerCard", playerCard); // 当前播放的基础信息
        req.setAttribute("movieDetailData", detailData); // 当前播放的详情数据
        req.setAttribute("movieDetailDataPlayUrls", detailData.getPlayUrls()); // 当前播放页的播放地址数据

        req.getRequestDispatcher("/WEB-INF/views/home/movie_detail.jsp").forward(req, resp);
    }
}
